<%@page import="hibari.server.service.WeekService"%>
<%@page import="hibari.client.service.IWeekService"%>
<%@page import="com.google.gwt.core.client.GWT"%>
<%@page import="hibari.client.service.IMusicService"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="com.google.appengine.api.datastore.PreparedQuery"%>
<%@page import="com.google.appengine.api.datastore.Query.SortDirection"%>
<%@page import="com.google.appengine.api.datastore.Query"%>
<%@page
	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="hibari.server.lib.AdminLog"%>
<%@page import="hibari.client.model.Music"%>
<%@page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="hibari.PMF"%>
<%@page import="hibari.client.model.Week"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String role = (String) session.getAttribute("role");
	if (role == null) {
		response.sendRedirect("login.jsp");
	}

	PersistenceManager pm = PMF.get().getPersistenceManager();

	String operation = "editWeek";
	Week week = null;
	Music music1 = null;
	Music music2 = null;

	String id = request.getParameter("id");
	if (id != null) {
		try {
			week = pm.getObjectById(Week.class, Long.parseLong(id));
			music1 = pm.getObjectById(Music.class, week.getMusic1());
			music2 = pm.getObjectById(Music.class, week.getMusic2());
		} catch (Exception e) {
			AdminLog.get(session).addError(
					"Could not find Week with id : " + id);
			response.sendRedirect("/Admin/index.jsp");
		}
	} else {
		week = new Week();

		IWeekService weekService = new WeekService();
		Week lastWeek = weekService.getLastWeek();
		if (lastWeek != null) {
			week.setWeekNumber(lastWeek.getWeekNumber() + 1);
		} else {
			week.setWeekNumber(1);
		}

		music1 = new Music();
		music2 = new Music();

		operation = "newWeek";
	}

	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Edit Week</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="jscolor/jscolor.js"></script>
</head>
<body onload="main()">
	<%=AdminLog.get(session).render(session)%>
	<a href="/Admin/index.jsp">return to console</a>
	<form action="/edit" enctype="multipart/form-data" method="post">
		<input type="hidden" name="operation" value="<%=operation%>">
		<input type="hidden" name="id" value="<%=id%>" /> <input
			type="hidden" name="callback" value="/Admin/WeekList.jsp" />
		<fieldset>
			<legend>Week Info</legend>
			<table>
				<tr>
					<th>date</th>
					<td><input type="date" name="date"
						value="<%=dateFormat.format(week.getDate())%>" />
					</td>
				</tr>
				<tr>
					<th>Week No.</th>
					<td><input type="integer" name="week_no"
						value="<%=week.getWeekNumber()%>" />
					</td>
				</tr>

				<tr>
					<th rowspan="2">Image</th>
					<td><img id="week_image_preview"
						src="/image?id=<%=week.getImageId()%>" /></td>
				</tr>
				<tr>
					<td>
						<input id="week_image" name="week_image" type="file" /> 
						<input id="remove_week_image" name="remove_week_image" type="checkbox" value="remove" /><span id="remove_week_image_label" >remove current image
							<script type="text/javascript">
								$("#remove_week_image_label").click(function(){
									var c = $("#remove_week_image");
									if(c.attr("checked")){
										c.removeAttr("checked");
									}else{
										c.attr("checked", "checked");
									}
								});
							</script>
						</span> 
					 </td>
				</tr>
				<tr>
					<th>background-color</th>
					<td><input id="week_color" name="week_color" class="color"  value="<%=week.getBackgroundColor() %>" /></td>
				</tr>
			</table>
		</fieldset>

		<fieldset class="fold">
			<legend>Music 1</legend>
			<table>
				<tr>
					<th>Title(EN)</th>
					<td><input type="text" name="music1_title_en"
						value="<%=music1.getTitleInEnglish()%>" /></td>
					<th>Title(JP)</th>
					<td><input type="text" name="music1_title_jp"
						value="<%=music1.getTitleInJapanese()%>" /></td>
				</tr>
				<tr>
					<th rowspan="2">Image</th>
					<td colspan="3"><img src="/image?id=<%=music1.getImageId()%>" />
					</td>
				</tr>
				<tr>
					<td colspan="3"><input name="music1_image" type="file" /></td>
				</tr>

				<tr>
					<th>Composer(EN)</th>
					<td><input type="text" name="music1_composer_en"
						value="<%=music1.getComposerInEnglish()%>" /></td>
					<th>Composer(JP)</th>
					<td><input type="text" name="music1_composer_jp"
						value="<%=music1.getComposerInJapanese()%>" /></td>
				</tr>

				<tr>
					<th>Composer Profile(EN)</th>
					<td><textarea name="music1_composer_profile_en"><%=music1.getComposerProfileInEnglish()%></textarea>
					</td>
					<th>Composer Profile(JP)</th>
					<td><textarea name="music1_composer_profile_jp"><%=music1.getComposerProfileInJapanese()%></textarea>
					</td>
				</tr>

				<tr>
					<th>Composition(EN)</th>
					<td><input type="text" name="music1_composition_en"
						value="<%=music1.getCompositionInEnglish()%>" /></td>
					<th>Composition(JP)</th>
					<td><input type="text" name="music1_composition_jp"
						value="<%=music1.getCompositionInJapanese()%>" /></td>
				</tr>

				<tr>
					<th>Description(EN)</th>
					<td><textarea name="music1_description_en"><%=music1.getDescriptionInEnglish()%></textarea>
					</td>
					<th>Description(JP)</th>
					<td><textarea name="music1_description_jp"><%=music1.getDescriptionInJapanese()%></textarea>
					</td>
				</tr>

				<tr>
					<th>Demo URL</th>
					<td colspan="1"><input type="text" name="music1_demo_url"
						value="<%=music1.getDemoUrl()%>" /></td>
					<td colspan="2"><audio controls="controls"> <source
							src="<%=music1.getDemoUrl()%>" type="audio/mp3" /> </audio></td>
				</tr>
			</table>
		</fieldset>

		<fieldset class="fold">
			<legend>Music 2</legend>
			<table>
				<tr>
					<th>Title(EN)</th>
					<td><input type="text" name="music2_title_en"
						value="<%=music2.getTitleInEnglish()%>" /></td>
					<th>Title(JP)</th>
					<td><input type="text" name="music2_title_jp"
						value="<%=music2.getTitleInJapanese()%>" /></td>
				</tr>
				<tr>
					<th rowspan="2">Image</th>
					<td colspan="3"><img src="/image?id=<%=music2.getImageId()%>" />
					</td>
				</tr>
				<tr>
					<td colspan="3"><input name="music2_image" type="file" /></td>
				</tr>
				<tr>
					<th>Composer(EN)</th>
					<td><input type="text" name="music2_composer_en"
						value="<%=music2.getComposerInEnglish()%>" /></td>
					<th>Composer(JP)</th>
					<td><input type="text" name="music2_composer_jp"
						value="<%=music2.getComposerInJapanese()%>" /></td>
				</tr>

				<tr>
					<th>Composer Profile(EN)</th>
					<td><textarea name="music2_composer_profile_en"><%=music2.getComposerProfileInEnglish()%></textarea>
					</td>
					<th>Composer Profile(JP)</th>
					<td><textarea name="music2_composer_profile_jp"><%=music2.getComposerProfileInJapanese()%></textarea>
					</td>
				</tr>

				<tr>
					<th>Composition(EN)</th>
					<td><input type="text" name="music2_composition_en"
						value="<%=music2.getCompositionInEnglish()%>" /></td>
					<th>Composition(JP)</th>
					<td><input type="text" name="music2_composition_jp"
						value="<%=music2.getCompositionInJapanese()%>" /></td>
				</tr>

				<tr>
					<th>Description(EN)</th>
					<td><textarea name="music2_description_en"><%=music2.getDescriptionInEnglish()%></textarea>
					</td>
					<th>Description(EN)</th>
					<td><textarea name="music2_description_jp"><%=music2.getDescriptionInJapanese()%></textarea>
					</td>
				</tr>

				<tr>
					<th>Demo URL</th>
					<td colspan="1"><input type="text" name="music2_demo_url"
						value="<%=music2.getDemoUrl()%>" /></td>
					<td colspan="2"><audio controls="controls"> <source
							src="<%=music2.getDemoUrl()%>" type="audio/mp3" /> </audio></td>
				</tr>
			</table>
		</fieldset>
		<input type="submit" />
	</form>

	<%
		if (id != null) {
	%>
	<form action="/edit" enctype="multipart/form-data" method="post">
		<input type="hidden" name="callback" value="/Admin/WeekList.jsp" /> <input
			type="hidden" name="operation" value="removeWeek"> <input
			type="hidden" name="id" value="<%=week.getKey()%>" /> <input
			type="submit" value="Delete" class="delete-button" />
	</form>
	<%
		}
	%>
</body>

</html>